/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.ucagent.database.dao;

import cn.net.greenet.db.HibernateUtil;
import java.util.ArrayList;
import java.util.List;

import cn.net.greenet.db.bean.Messageincominglog;
import cn.net.greenet.db.bean.Messageoutgoinglog;
import cn.net.greenet.db.bean.MessageoutgoinglogManual;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author hedingwei
 */
public class MessageDao {

    public List<Object[]> getMessageoutgoinglogManual(String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if (messageType == null) {
                query = session.createQuery("FROM MessageoutgoinglogManual ORDER BY id desc");
            } else {
                query = session.createQuery("SELECT log FROM MessageoutgoinglogManual AS log where log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            MessageoutgoinglogManual mm = null;
            for (Object obj : l) {
                mm = (MessageoutgoinglogManual) obj;
                Query q = session.createQuery("SELECT ack FROM Ackincominglog AS ack where ack.messageType=:messageType and ack.messageNo=:messageNo and ack.messageSequenceNo=:messageSequenceNo and ack.messageSerialNo=:messageSerialNo");
                q.setParameter("messageType", mm.getMessageType());
                q.setParameter("messageNo", mm.getMessageNo());
                q.setParameter("messageSequenceNo", mm.getMessageSequenceNo());
                q.setParameter("messageSerialNo", mm.getMessageSerialNo());
                List acks = q.list();
                if (acks.isEmpty()) {
                    beans.add(new Object[]{obj, null});
                } else {
                    beans.add(new Object[]{obj, acks});
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageoutgoinglogManualByDPI(int dpiid, String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if (messageType == null) {
                query = session.createQuery("SELECT log FROM MessageoutgoinglogManual AS log where log.dpiendpointbean.id=:dpiid ORDER BY id desc");
            } else {
                query = session.createQuery("SELECT log FROM MessageoutgoinglogManual AS log where log.dpiendpointbean.id=:dpiid and log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }

            query.setParameter("dpiid", dpiid);
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            MessageoutgoinglogManual mm = null;
            for (Object obj : l) {
                mm = (MessageoutgoinglogManual) obj;
                Query q = session.createQuery("SELECT ack FROM Ackincominglog AS ack where ack.messageType=:messageType and ack.messageNo=:messageNo");
                q.setParameter("messageType", mm.getMessageType());
                q.setParameter("messageNo", mm.getMessageNo());
                q.setParameter("messageSequenceNo", mm.getMessageSequenceNo());
                q.setParameter("messageSerialNo", mm.getMessageSerialNo());
                List acks = q.list();
                if (acks.isEmpty()) {
                    beans.add(new Object[]{obj, null});
                } else {
                    beans.add(new Object[]{obj, acks});
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageoutgoinglogManualByDPI(String name, String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if (messageType == null) {
                query = session.createQuery("SELECT log FROM MessageoutgoinglogManual AS log where log.dpiendpointbean.name=:name ORDER BY id desc");
            } else {
                query = session.createQuery("SELECT log FROM MessageoutgoinglogManual AS log where log.dpiendpointbean.name=:name and log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setParameter("name", name);
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            MessageoutgoinglogManual mm = null;
            for (Object obj : l) {
                mm = (MessageoutgoinglogManual) obj;
                Query q = session.createQuery("SELECT ack FROM Ackincominglog AS ack where ack.messageType=:messageType and ack.messageNo=:messageNo and ack.messageSequenceNo=:messageSequenceNo");
                q.setParameter("messageType", mm.getMessageType());
                q.setParameter("messageNo", mm.getMessageNo());
                q.setParameter("messageSequenceNo", mm.getMessageSequenceNo());
                q.setParameter("messageSerialNo", mm.getMessageSerialNo());
                List acks = q.list();
                if (acks.isEmpty()) {
                    beans.add(new Object[]{obj, null});
                } else {
                    beans.add(new Object[]{obj, acks});
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageoutgoinglog(String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if (messageType == null) {
                query = session.createQuery("FROM Messageoutgoinglog ORDER BY id desc");
            } else {
                query = session.createQuery("SELECT log FROM Messageoutgoinglog AS log where log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            Messageoutgoinglog mm = null;
            for (Object obj : l) {
                mm = (Messageoutgoinglog) obj;
                Query q = session.createQuery("SELECT ack FROM Ackincominglog AS ack where ack.messageType=:messageType and ack.messageNo=:messageNo and ack.messageSequenceNo=:messageSequenceNo");
                q.setParameter("messageType", mm.getMessageType());
                q.setParameter("messageNo", mm.getMessageNo());
                q.setParameter("messageSequenceNo", mm.getMessageSequenceNo());
                q.setParameter("messageSerialNo", mm.getMessageSerialNo());
                List acks = q.list();
                if (acks.isEmpty()) {
                    beans.add(new Object[]{obj, null});
                } else {
                    beans.add(new Object[]{obj, acks});
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageoutgoinglogByDPI(int dpiid, String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if(messageType==null){
                query = session.createQuery("SELECT log FROM Messageoutgoinglog AS log where log.dpiendpointbean.id=:dpiid ORDER BY id desc");
            }else{
                query = session.createQuery("SELECT log FROM Messageoutgoinglog AS log where log.dpiendpointbean.id=:dpiid and log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setParameter("dpiid", dpiid);
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            Messageoutgoinglog mm = null;
            for (Object obj : l) {
                mm = (Messageoutgoinglog) obj;
                Query q = session.createQuery("SELECT ack FROM Ackincominglog AS ack where ack.messageType=:messageType and ack.messageNo=:messageNo and ack.messageSequenceNo=:messageSequenceNo");
                q.setParameter("messageType", mm.getMessageType());
                q.setParameter("messageNo", mm.getMessageNo());
                q.setParameter("messageSequenceNo", mm.getMessageSequenceNo());
                q.setParameter("messageSerialNo", mm.getMessageSerialNo());
                List acks = q.list();
                if (acks.isEmpty()) {
                    beans.add(new Object[]{obj, null});
                } else {
                    beans.add(new Object[]{obj, acks});
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageoutgoinglogByDPI(String name, String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if(messageType==null){
                query = session.createQuery("SELECT log FROM Messageoutgoinglog AS log where log.dpiendpointbean.name=:name ORDER BY id desc");
            }else{
                query = session.createQuery("SELECT log FROM Messageoutgoinglog AS log where log.dpiendpointbean.name=:name and messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setParameter("name", name);
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            Messageoutgoinglog mm = null;
            for (Object obj : l) {
                mm = (Messageoutgoinglog) obj;
                Query q = session.createQuery("SELECT ack FROM Ackincominglog AS ack where ack.messageType=:messageType and ack.messageNo=:messageNo and ack.messageSequenceNo=:messageSequenceNo");
                q.setParameter("messageType", mm.getMessageType());
                q.setParameter("messageNo", mm.getMessageNo());
                q.setParameter("messageSequenceNo", mm.getMessageSequenceNo());
                q.setParameter("messageSerialNo", mm.getMessageSerialNo());
                List acks = q.list();
                if (acks.isEmpty()) {
                    beans.add(new Object[]{obj, null});
                } else {
                    beans.add(new Object[]{obj, acks});
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageincominglog(String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if(messageType==null){
                query = session.createQuery("FROM Messageincominglog ORDER BY id desc");
            }else{
                query = session.createQuery("SELECT log FROM Messageincominglog AS log where log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            Messageincominglog mm = null;
            for (Object obj : l) {
                mm = (Messageincominglog) obj;
                beans.add(new Object[]{obj, mm.getAckoutgoinglogs()});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageincominglogByDPI(int dpiid,String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if(messageType==null){
                query = session.createQuery("SELECT log FROM Messageincominglog AS log where log.dpiendpointbean.id=:dpiid ORDER BY id desc");
            }else{
                query = session.createQuery("SELECT log FROM Messageincominglog AS log where log.dpiendpointbean.id=:dpiid where log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            
            query.setParameter("dpiid", dpiid);
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            Messageincominglog mm = null;
            for (Object obj : l) {
                mm = (Messageincominglog) obj;
                beans.add(new Object[]{obj, mm.getAckoutgoinglogs()});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    public List<Object[]> getMessageincominglogByDPI(String name,String messageType, int page, int size) {
        List<Object[]> beans = new ArrayList<Object[]>();
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = null;
            if(messageType==null){
                query = session.createQuery("SELECT log FROM Messageincominglog AS log where log.dpiendpointbean.name=:name ORDER BY id desc");
            }else{
                query = session.createQuery("SELECT log FROM Messageincominglog AS log where log.dpiendpointbean.name=:name and log.messageType=:messageType ORDER BY id desc");
                query.setParameter("messageType", messageType);
            }
            query.setParameter("name", name);
            query.setFirstResult(page * size);
            query.setMaxResults(size);
            List l = query.list();
            Messageincominglog mm = null;
            for (Object obj : l) {
                mm = (Messageincominglog) obj;
                beans.add(new Object[]{obj, mm.getAckoutgoinglogs()});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return beans;
    }

    
    
}
